home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / pygtk / 2.0 / demos / dnd.py < prev    next >
Text File  |  2006-01-20  |  7KB  |  205 lines

  1. #!/usr/bin/env python
  2. '''Drag and Drop
  3.  
  4. This is a test of the drag and drop capabilities of gtk.  It is a
  5. fairly straight forward port of the example distributed with gtk.
  6. '''
  7.  
  8. import gtk
  9. import gobject
  10. from dndpixmap import *
  11.  
  12. TARGET_STRING = 0
  13. TARGET_ROOTWIN = 1
  14.  
  15. target = [
  16.     ('STRING', 0, TARGET_STRING),
  17.     ('text/plain', 0, TARGET_STRING),
  18.     ('application/x-rootwin-drop', 0, TARGET_ROOTWIN)
  19. ]
  20.  
  21. def create_pixmap(widget, xpm_data):
  22.     return \
  23.         gtk.gdk.pixmap_colormap_create_from_xpm_d(
  24.             None, widget.get_colormap(), None, xpm_data)
  25.  
  26. class DragAndDropDemo(gtk.Window):
  27.     trashcan_open = None
  28.     trashcan_open_mask = None
  29.     trashcan_closed = None
  30.     trashcan_closed_mask = None
  31.     drag_icon = None
  32.     drag_mask = None
  33.     have_drag = False
  34.     popped_up = False
  35.     in_popup = False
  36.     popup_timer = 0
  37.     popdown_timer = 0
  38.     popup_win = None
  39.  
  40.     def __init__(self, parent=None):
  41.         gtk.Window.__init__(self)
  42.         try:
  43.             self.set_screen(parent.get_screen())
  44.         except AttributeError:
  45.             self.connect('destroy', lambda *w: gtk.main_quit())
  46.         self.set_title(self.__class__.__name__)
  47.  
  48.         table = gtk.Table(2,2)
  49.         self.add(table)
  50.  
  51.         self.drag_icon, self.drag_mask = \
  52.             create_pixmap(self, drag_icon_xpm)
  53.         self.trashcan_open, self.trashcan_open_mask = \
  54.             create_pixmap(self, trashcan_open_xpm)
  55.         self.trashcan_closed, self.trashcan_closed_mask = \
  56.             create_pixmap(self, trashcan_closed_xpm)
  57.  
  58.         label = gtk.Label('Drop to Trashcan!\n')
  59.         label.drag_dest_set(gtk.DEST_DEFAULT_ALL, target[:-1],
  60.                 gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  61.         label.connect('drag_data_received', self.label_drag_data_received)
  62.         table.attach(label, 0, 1, 0, 1)
  63.  
  64.         label = gtk.Label('Popup\n')
  65.         label.drag_dest_set(gtk.DEST_DEFAULT_ALL, target[:-1],
  66.                 gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  67.         table.attach(label, 1, 2, 1, 2)
  68.         label.connect('drag_motion', self.popsite_motion)
  69.         label.connect('drag_leave', self.popsite_leave)
  70.  
  71.         image = gtk.Image()
  72.         image.set_from_pixmap(self.trashcan_closed, self.trashcan_closed_mask)
  73.         image.drag_dest_set(0, [], 0)
  74.         table.attach(image, 1, 2, 0, 1)
  75.         image.connect('drag_leave', self.target_drag_leave)
  76.         image.connect('drag_motion', self.target_drag_motion)
  77.         image.connect('drag_drop', self.target_drag_drop)
  78.         image.connect('drag_data_received', self.target_drag_data_received)
  79.  
  80.         b = gtk.Button('Drag from Here\n')
  81.         b.drag_source_set(gtk.gdk.BUTTON1_MASK | gtk.gdk.BUTTON3_MASK,
  82.                   target, gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  83.         b.drag_source_set_icon(self.get_colormap(), self.drag_icon, self.drag_mask)
  84.         table.attach(b, 0, 1, 1, 2)
  85.         b.connect('drag_data_get', self.source_drag_data_get)
  86.         b.connect('drag_data_delete', self.source_drag_data_delete)
  87.         self.show_all()
  88.  
  89.     def label_drag_data_received(self, w, context, x, y, data, info, time):
  90.         if data and data.format == 8:
  91.             print 'Received "%s" in label' % data.data
  92.             context.finish(True, False, time)
  93.         else:
  94.             context.finish(False, False, time)
  95.  
  96.     def popsite_motion(self, w, context, x, y, time):
  97.         if not self.popup_timer:
  98.             self.popup_timer = gobject.timeout_add(500, self.popup_cb)
  99.         return True
  100.  
  101.     def popsite_leave(self, w, context, time):
  102.         if self.popup_timer:
  103.             gobject.source_remove(self.popup_timer)
  104.             self.popup_timer = 0
  105.  
  106.     def popup_motion(self, w, context, x, y, time):
  107.         print 'popup_motion'
  108.         if not self.in_popup:
  109.             self.in_popup = True
  110.             if self.popdown_timer:
  111.                 print 'removed popdown'
  112.                 gobject.source_remove(self.popdown_timer)
  113.                 self.popdown_timer = 0
  114.         return True
  115.  
  116.     def popup_leave(self, w, context, time):
  117.         print 'popup_leave'
  118.         if self.in_popup:
  119.             self.in_popup = False
  120.             if not self.popdown_timer:
  121.                 print 'added popdown'
  122.                 self.popdown_timer = gobject.timeout_add(500, self.popdown_cb)
  123.  
  124.     def popup_cb(self):
  125.         if not self.popped_up:
  126.             if self.popup_win is None:
  127.                 self.popup_win = gtk.Window(gtk.WINDOW_POPUP)
  128.                 self.popup_win.set_position(gtk.WIN_POS_MOUSE)
  129.                 table = gtk.Table(3, 3)
  130.                 for k in range(9):
  131.                     i, j = divmod(k, 3)
  132.                     b = gtk.Button("%d,%d" % (i,j))
  133.                     b.drag_dest_set(gtk.DEST_DEFAULT_ALL, target[:-1],
  134.                         gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  135.                     b.connect('drag_motion', self.popup_motion)
  136.                     b.connect('drag_leave', self.popup_leave)
  137.                     table.attach(b, i, i+1, j, j+1)
  138.                 table.show_all()
  139.                 self.popup_win.add(table)
  140.             self.popup_win.present()
  141.             self.popped_up = True
  142.         self.popdown_timer = gobject.timeout_add(500, self.popdown_cb)
  143.         print 'added popdown'
  144.         self.popup_timer = 0
  145.         return False
  146.  
  147.     def popdown_cb(self):
  148.         print 'popdown'
  149.         #if self.in_popup:
  150.         #    return True
  151.         self.popdown_timer = 0
  152.         self.popup_win.hide()
  153.         self.popped_up = False
  154.         return False
  155.  
  156.     def target_drag_leave(self, img, context, time):
  157.         print 'leave'
  158.         self.have_drag = False
  159.         img.set_from_pixmap(self.trashcan_closed, self.trashcan_closed_mask)
  160.  
  161.     def target_drag_motion(self, img, context, x, y, time):
  162.         if self.have_drag is False:
  163.             self.have_drag = True
  164.             img.set_from_pixmap(self.trashcan_open, self.trashcan_open_mask)
  165.         source_widget = context.get_source_widget()
  166.         print 'motion, source ',
  167.         if source_widget:
  168.             print source_widget.__class__.__name__
  169.         else:
  170.             print 'unknown'
  171.         context.drag_status(context.suggested_action, time)
  172.         return True
  173.  
  174.     def target_drag_drop(self, img, context, x, y, time):
  175.         print 'drop'
  176.         self.have_drag = False
  177.         img.set_from_pixmap(self.trashcan_closed, self.trashcan_closed_mask)
  178.         if context.targets:
  179.             img.drag_get_data(context, context.targets[0], time)
  180.             return True
  181.         return False
  182.  
  183.     def target_drag_data_received(self, img, context, x, y, data, info, time):
  184.         if data.format == 8:
  185.             print 'Received "%s" in trashcan' % data.data
  186.             context.finish(True, False, time)
  187.         else:
  188.             context.finish(False, False, time)
  189.  
  190.     def source_drag_data_get(self, btn, context, selection_data, info, time):
  191.         if info == TARGET_ROOTWIN:
  192.             print 'I was dropped on the rootwin'
  193.         else:
  194.             selection_data.set(selection_data.target, 8, "I'm Data!")    
  195.  
  196.     def source_drag_data_delete(self, btn, context, data):
  197.         print 'Delete the data!'
  198.  
  199. def main():
  200.     DragAndDropDemo()
  201.     gtk.main()
  202.  
  203. if __name__ == '__main__':
  204.     main()
  205.